package com.bdd.kdi;

import java.util.Locale;

import com.example.kdi.dummy.ProduitPrix;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.Html;
import android.util.Log;

public class ListFinalBDD extends DataBaseHelper{
	
	private static final String TABLE_LIST = "list";
		
	public ListFinalBDD(Context context) {
		super(context);
		myDataBase = this.getWritableDatabase();
	}
	
	public void creatList(){
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_LIST + " AS SELECT * FROM table_final LIMIT 0";
		myDataBase.execSQL(CREATE_CONTACTS_TABLE) ;
	}
	
	public void deleteList(){
	
		myDataBase.delete(TABLE_LIST, null, null);
	}
	
	
	
	
	public void addProduitPrix(ProduitPrix Produit, int nbr) {
	    ContentValues values = new ContentValues();
	    
	    String idprod = Produit.getId();
	    String lib = Produit.getLibelle();
	    String typ = Produit.getId_typeprod();
	    String idEns = Produit.getId_enseigne();
	    String px = String.valueOf(Produit.getPrix());
	    String nb = String.valueOf(nbr);
	    	    
	    values.put("_id_produit", Integer.valueOf(idprod));
	    values.put("_id_typeprod", typ);
	    values.put("typlibelle", "");
	    values.put("_id_enseigne", Integer.valueOf(idEns));
	    values.put("libelle", lib); 
	    values.put("prix", Double.valueOf(px));	  
	    values.put("nb",Integer.valueOf(nb));
	    
	 
	    String stwhere = new String("_id_produit = " + idprod + " and _id_typeprod = " + typ + " and _id_enseigne = " + idEns  );
	    if (checkRowExist(idprod,typ,idEns))
	    	myDataBase.update(TABLE_LIST, values,stwhere , null);
	    else
	    	myDataBase.insertOrThrow(TABLE_LIST, null, values);
	    
	}
	
	public boolean  deleteProduitPrix(String idProd,String typ,String idEns){
	
		String where = "_id_produit  = " + Integer.valueOf(idProd) 
		    + " AND _id_typeprod = " + Integer.valueOf(typ) 
		    + " AND _id_enseigne = " + Integer.valueOf(idEns);
		String[] whereArgs = null;		
		return myDataBase.delete(TABLE_LIST, where, whereArgs) > 0;
	}
	
	public boolean checkRowExist(String idprod,String typ,String idEns ){
		String sql = "SELECT * FROM list WHERE _id_produit = " + idprod + " and _id_typeprod = " + typ + " and _id_enseigne = " + idEns +";";
		Cursor data = myDataBase.rawQuery(sql, null);
		boolean exists = (data.getCount() > 0);
		data.close();
		return exists;		
	}
	
}
